<?php

/**
 * Created by PhpStorm.
 * User: ty
 * Date: 2025/8/14
 * Time: 10:08
 *  z_name 姓名
 *  mobile 手机号码
 *
 *
 */

/*
var_dump(Mock::z_name());
var_dump(Mock::mobile());
var_dump(Mock::carId());
var_dump(Mock::rint(1,10));


根据规则创建

$rules=[
    'group_id'=>5,
    'fangwenuser_id'=>11,
    'user_id'=>10,
    'name'=>['z_name'],
    'mobile'=>['mobile'],
    'person_number'=>['rint','1','10'],
    'fangwen_time'=>['date','2025-08-14','2025-09-14'],
    'shiyou'=>['list',['沟通合作','工程结算']],
    'car_ids'=>['carId'],
    'createtime'=>time()

];

$res=Mock::creatByrules($rules);

$res=Mock::ceartSqlByrules('yuyue',$rules,30);

*/

class Mock
{
    public static function z_name()
    {
        // 定义名字和姓氏的数组
        $firstNames = [
            "张", "李", "王", "赵", "周", "吴", "郑", "钱", "孙", "陈",
            "刘", "黄", "杨", "徐", "叶", "朱", "高", "林", "何", "郭"
        ];

        $lastNames = [
            "志明", "美丽", "强", "磊", "燕", "芳", "娜", "静", "飞", "洋"
        ];

        // 随机选择一个名字和姓氏
        $firstName = $firstNames[array_rand($firstNames)];
        $lastName = $lastNames[array_rand($lastNames)];

        // 组合成完整的姓名
        $fullName = $firstName . $lastName;

        return $fullName;
    }

    public static function mobile()
    {
        // 中国手机号前缀
        $prefixes = [
            '130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
            '150', '151', '152', '153', '155', '156', '157', '158', '159',
            '166', '170', '171', '172', '173', '174', '175', '176', '177', '178',
            '180', '181', '182', '183', '184', '185', '186', '187', '188', '189',
            '191', '198', '199'
        ];

        // 随机选择前缀
        $prefix = $prefixes[array_rand($prefixes)];

        // 生成后8位随机数
        $suffix = '';
        for ($i = 0; $i < 8; $i++) {
            $suffix .= mt_rand(0, 9);
        }

        return $prefix . $suffix;
    }

    public static function carId()
    {
        // 省份简称
        $provinces = [
            '京', '津', '冀', '晋', '蒙', '辽', '吉', '黑',
            '沪', '苏', '浙', '皖', '闽', '赣', '鲁', '豫',
            '鄂', '湘', '粤', '桂', '琼', '渝', '川', '贵',
            '云', '藏', '陕', '甘', '青', '宁', '新'
        ];

        // 字母部分
        $letters = 'ABCDEFGHJKLMNPQRSTUVWXYZ'; // 不含I和O

        // 数字部分
        $numbers = '0123456789';

        // 生成车牌号
        $plate = '';

        // 1. 省份简称
        $plate .= $provinces[array_rand($provinces)];

        // 2. 地区字母
        $plate .= $letters[rand(0, strlen($letters) - 1)];

        // 3. 5位数字/字母组合
        $chars = $numbers . $letters;
        for ($i = 0; $i < 5; $i++) {
            $plate .= $chars[rand(0, strlen($chars) - 1)];
        }

        return $plate;
    }

    public static function date($startDate, $endDate)
    {
        $startTimestamp = strtotime($startDate);
        $endTimestamp = strtotime($endDate);

        if ($startTimestamp > $endTimestamp) {
            throw new InvalidArgumentException("结束日期不能早于开始日期");
        }

        $randomTimestamp = mt_rand($startTimestamp, $endTimestamp);
        return date('Y-m-d', $randomTimestamp);
    }

    public static function list($items)
    {
        if (empty($items)) {
            throw new InvalidArgumentException("数组不能为空");
        }
        return $items[array_rand($items)];
    }
    
    public static function rint(int $min, int $max)
    {
        // 参数验证
        if ($min > $max) {
            throw new InvalidArgumentException('最小值不能大于最大值');
        }

        // 使用random_int生成加密安全的随机数
        return random_int($min, $max);
    }

    //通过规则创建
    public static function creatByrules($rules)
    {
        $data=[];
        foreach($rules as $key=>$item){
            if(!is_array($item)){
               $data[$key]=$item;
               continue;
            }else{
                $params = array_slice($item, 1, count($item));
                $data[$key]=call_user_func([self::class, $item[0]], ...$params);
            }
        }

        return $data;
    }

    public static function arrayToInsertSQL(string $table, array $datalist){
        $str='';
        foreach($datalist as $data){
            $columns = implode(", ", array_keys($data));
            $values = "'" . implode("', '", array_values($data)) . "'";
            $str.="INSERT INTO $table ($columns) VALUES ($values);";
        }
       return $str;
    }
    //根据规则生成sql语句
    public static function ceartSqlByrules($table,$rules,$num=1)
    {
        $datalist=[];
        for($i=1;$i<=$num;$i++){
            $data=self::creatByrules($rules);
            $datalist[]=$data;
        }
        //创建sql语句
        $sql=self::arrayToInsertSQL($table,$datalist);
        return $sql;
    }
}